package com.qianji.controller;

import java.security.Security;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import com.qianji.pojo.PVUVDto;
import com.qianji.vo.PVUVVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class SendEmailDemo {
    private static final String SMTP_HOST = "smtp.qiye.aliyun.com";

    private static final String SMTP_PORT = "25";// 或80


    /**
     * @param sendAddress 发件人地址
     * @param sendPassword 发件人密码
     * @param host 协议
     * @param port 端口
     * @param subject 标题
     * @param content 内容
     * @param filePath 附件地址
     * @param CC 抄送人
     * @throws Exception
     * @throws AddressException
     */
    public static void sendMail(String sendAddress,String sendPassword,String host,String port,String subject,String content,String internetAddress,String filePath,String CC) throws AddressException, Exception {
        //设置SSL连接、邮件环境
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", host);
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", port);//设置端口
        props.setProperty("mail.debug", "true");//启用调试
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.auth", "true");
        //建立邮件会话
        Session session = Session.getDefaultInstance(props, new Authenticator() {   //身份认证
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(sendAddress, sendPassword);//发件人账号、密码
            }
        });
        //建立邮件对象
        MimeMessage message = new MimeMessage(session);
        //设置邮件的发件人、收件人、主题
        //附带发件人名字
        //  message.setFrom(new InternetAddress("from_mail@qq.com", "optional-personal"));
        message.setFrom(new InternetAddress(sendAddress));//发件人账号
        message.setRecipients(Message.RecipientType.TO, internetAddress);//收件人账号

        //标题
        message.setSubject(subject);//邮件标题

        //内容
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(content, "text/html;charset=utf-8");//邮件内容
        multipart.addBodyPart(contentPart);

        //附件部分
        if (StringUtils.isNotBlank(filePath)) {
            BodyPart attachPart = new MimeBodyPart();
            FileDataSource fileDataSource = new FileDataSource(filePath);//附件地址 D:/题库上传模板v1.xlsx
            attachPart.setDataHandler(new DataHandler(fileDataSource));
            attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
            multipart.addBodyPart(attachPart);
        }

        message.setContent(multipart);

        //抄送地址
        if (StringUtils.isNotBlank(CC)) {
            InternetAddress[] internetAddressCC = new InternetAddress().parse(CC);
            message.setRecipients(Message.RecipientType.CC, internetAddressCC);
        }

        //发送邮件
        Transport.send(message);
    }
    public static void main(String[] args) {
        /*try {
            sendMail("zhonglishi@allinonemedia.cn", "qianji_2021", SMTP_HOST, SMTP_PORT, "subject test",
                    "<H1>AAAA</H1>", "2946221370@qq.com", null, null);
        } catch (Exception e) {
            System.out.println("出现异常");
        }*/

        /*{"timestamp": "1624526325000", "data": {"imei": "16239177579811396959", "activeId": 66, "linkUrl": "https://www.baidu.com", "panId": 216,"linkId":127}}*/
        
    }
}

